package com.sc.outpatientsys.service.impl;

import com.sc.outpatientsys.entity.Pharmaceutist;
import com.sc.outpatientsys.mapper.PharmaceutistMapper;
import com.sc.outpatientsys.service.IPharmaceutistService;
import com.sc.outpatientsys.service.ex.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class PharmaceutistServiceImpl implements IPharmaceutistService {
    @Autowired
    private PharmaceutistMapper pharmaceutistMapper;
    @Override
    public void reg(Pharmaceutist pharmaceutist) {
        String username = pharmaceutist.getUserName();
        Pharmaceutist res = pharmaceutistMapper.findPharmaceutistByName(username);
        if(res!=null){
            throw new UserNameHasExistException("用户名已存在");
        }
        Integer rows = pharmaceutistMapper.insertPharmaceutist(pharmaceutist);
        if(rows!=1){
            throw new InsertException("用户注册时出现未知错误");
        }
    }

    @Override
    public Pharmaceutist login(String username, String password) {
        log.info(username+" :: "+password);
        Pharmaceutist res = pharmaceutistMapper.findPharmaceutistByName(username);
        if(res!=null && password.equals(res.getPassword())){
            return res;
        }else {
            throw new PasswordNotMacthException("用户名密码错误");
        }
    }

    @Override
    public void update(Pharmaceutist pharmaceutist) {
        Integer res = pharmaceutistMapper.updatePharmaceutist(pharmaceutist);
        if(res != 1){
            throw new UpdateException("修改时发生错误");
        }
    }

    @Override
    public void delete(int ph_ID) {
        Integer res = pharmaceutistMapper.deletePharmaceutist(ph_ID);
        if(res != 1){
            throw new DeleteException("删除时发生错误");
        }
    }

    @Override
    public Pharmaceutist findById(int ph_ID) {
        Pharmaceutist res = pharmaceutistMapper.findPharmaceutistByID(ph_ID);
        if(res == null){
            throw new NotFindException("未找到");
        }
        return res;
    }

    @Override
    public Pharmaceutist findByName(String username) {
        Pharmaceutist res = pharmaceutistMapper.findPharmaceutistByName(username);
        if(res == null){
            throw new NotFindException("未找到");
        }
        return res;
    }

    @Override
    public List<Pharmaceutist> findAll() {
        List<Pharmaceutist> res = pharmaceutistMapper.findAllPharmaceutist();
        if(res == null){
            throw new NotFindException("未找到");
        }
        return res;
    }
}
